<?php

// Get values from query string
//$day 	= $_GET["day"];
$month 	= $_GET["month"];

$year 	= $_GET["year"];
$sel 	= $_GET["sel"];
$what 	= $_GET["what"];
$field 	= $_GET["field"];
$form 	= $_GET["form"];

//if($day == "")
//$day = date("j");
$datelist   = array(
              '1' => '31',
              '2' => '1',
              '3' => '31',
              '4' => '30',
              '5' => '31',
              '6' => '30',
              '7' => '31',
              '8' => '31',
              '9' => '30',
              '10' => '31',
              '11' => '30',
              '12' => '31',
              );

              
if($month == "")
{
    $month = date("m");
    $month = intval($month);
}

if($year == "")
$year = date("Y");

//For February


$day = $datelist[$month];

$currentTimeStamp = strtotime("$year-$month-$day");
$monthName = date("F", $currentTimeStamp);
$numDays = date("t", $currentTimeStamp);
$counter = 0;

$monthlist	= array(
			'01' => 'Jan',
			'02' => 'Feb',
			'03' => 'Mar',
			'04' => 'Apr',
			'05' => 'May',
			'06' => 'Jun',
			'07' => 'Jul',
			'08' => 'Aug',
			'09' => 'Sep',
			'10' => 'Oct',
			'11' => 'Nov',
			'12' => 'Dec');

?>
<html>
<head>
<title>MyCalendar</title>
<link rel="stylesheet" type="text/css" href="calendar.css">
<script language="javascript" src="datelib.js"></script>
<script language="javascript">
        function goLastMonth(month,year,form,field)
        {
                // If the month is January, decrement the year.
                if(month == 1)
				{
				--year;
				month = 13;
				}
                document.location.href = 'calendar.php?month='+(month-1)+'&year='+year+'&form='+form+'&field='+field;
        }

        function goNextMonth(month,year,form,field)
        {
                // If the month is December, increment the year.
        
                if(month == 12)
        {
        ++year;
        month = 0;
        }
                document.location.href = 'calendar.php?month='+(month+1)+'&year='+year+'&form='+form+'&field='+field;
        }

        function sendToForm(val,field,form)
        {
		//alert (form + "," + field + "," + val);
                // Send back the date value to the form caller.
                chkday	= Left(val,2); //for 01 or 1
				
				if(chkday.indexOf("-") > -1) {
					val	= "0" + val;
				}
                
                eval("opener.document." + form + "." + field + ".value='" + val + "'");
                
                //eval("opener.document.forms[0]." + field + ".value='" + val + "'");
                window.close();
        }
</script>
</head>
<body style="margin:0px 0px 0px 0px" class="body">
<?
function print_header()
{
?>
	<tr>
			<td class='head' align="center" width='25'>S</td>
			<td class='head' align="center" width='25'>M</td>
			<td class='head' align="center" width='25'>T</td>
			<td class='head' align="center" width='25'>W</td>
			<td class='head' align="center" width='25'>T</td>
			<td class='head' align="center" width='25'>F</td>
			<td class='head' align="center" width='25'>S</td>
	</tr>

<?
}
?>
<table width='100%' border='0' cellspacing='5' cellpadding='5' class="body" align="center">
<tr>
	<td valign="top">
	<?
	  
		$prevmonth 	= $month-2;
		
		$prevyear	= $year;
		if ($prevmonth <= 0)
		{
			$prevmonth 	= 10 + $month;
			$prevyear 	= $year-1;
		} 
		
	?>
	<table width='175' border='0' cellspacing='0' cellpadding='0' class="body" align="center">
			<tr>
					<td width='25' colspan='1'>
					<input type='button' class='button' value=' < ' onClick='<?php echo "goLastMonth($prevmonth,$prevyear,\"$form\",\"$field\")"; ?>'>
					</td>
					<td width='125' align="center" colspan='5'>
					<span class='title'><?php echo $monthName . " " . $year; ?></span><br>
					</td>
					<td width='25' colspan='1' align='right'>
					</td>
			</tr>
			<? print_header() ?>
			<tr>
	<?
			for($i = 1; $i < $numDays+1; $i++, $counter++)
			{
					if (strlen($month) == 1)
						$month = "0".$month;

					$timeStamp = strtotime("$year-$month-$i");
					if($i == 1)
					{
					// Workout when the first day of the month is
					$firstDay = date("w", $timeStamp);

					for($j = 0; $j < $firstDay; $j++, $counter++)
					echo "<td> </td>";
					}

					if($counter % 7 == 0)
					echo "</tr><tr>";

					if(date("w", $timeStamp) == 0 || date("w", $timeStamp) == 6)

					$class = "class='weekend'";
					else
					if($i == date("d") && $month == date("m") && $year == date("Y"))
					$class = "class='today'";
					else
					$class = "class='normal'";

					echo "<td class='tr' bgcolor='#ffffff' align='center' width='25'><a class='buttonbar' href='#' onclick=\"sendToForm('$i-$monthlist[$month]-$year','$field','$form');\"><font $class>$i</font></a></td>";
			}
	?>
			</tr>
	</table>
	</td>

	<?
				
	$month++;
	$day = $datelist[$month];			
				

				if ($month == 13)
				{
 					$month 	= 1;
 					$year++;
				}
				$currentTimeStamp = strtotime("$year-$month-$day");
				$monthName = date("F", $currentTimeStamp);
				$numDays = date("t", $currentTimeStamp);
				$counter = 0;
	?>

	<td valign="top">
	<table width='175' border='0' cellspacing='0' cellpadding='0' class="body" align="center">
			<tr>
					<td width='25' colspan='1'>
					</td>
					<td width='125' align="center" colspan='5'>
					<span class='title'><?php echo $monthName . " " . $year; ?></span><br>
					</td>
					<td width='25' colspan='1' align='right'>
					</td>
			</tr>
			<? print_header() ?>
			<tr>
	<? 		$counter = 0;
			for($i = 1; $i < $numDays+1; $i++, $counter++)
			{
					if (strlen($month) == 1)
						$month = "0".$month;

					$timeStamp = strtotime("$year-$month-$i");
					if($i == 1)
					{
					// Workout when the first day of the month is
					$firstDay = date("w", $timeStamp);

					for($j = 0; $j < $firstDay; $j++, $counter++)
					echo "<td> </td>";
					}

					if($counter % 7 == 0)
					echo "</tr><tr>";

					if(date("w", $timeStamp) == 0 || date("w", $timeStamp) == 6)

					$class = "class='weekend'";
					else
					if($i == date("d") && $month == date("m") && $year == date("Y"))
					$class = "class='today'";
					else
					$class = "class='normal'";

					echo "<td class='tr' bgcolor='#ffffff' align='center' width='25'><a class='buttonbar' href='#' onclick=\"sendToForm('$i-$monthlist[$month]-$year','$field','$form');\"><font $class>$i</font></a></td>";
			}
	?>
			</tr>
	</table>
	</td>

	<?
				$month++;
				$day = $datelist[$month];
				if ($month == 13)
				{
					$month 	= 1;
					$year++;
				}
				$currentTimeStamp = strtotime("$year-$month-$day");
				
				$monthName = date("F", $currentTimeStamp);
				$numDays = date("t", $currentTimeStamp);
				$counter = 0;
	?>

	<td valign="top">
	<table width='175' border='0' cellspacing='0' cellpadding='0' class="body" align="center">
			<tr>
					<td width='25' colspan='1'>
					</td>
					<td width='125' align="center" colspan='5'>
					<span class='title'><?php echo $monthName . " " . $year; ?></span><br>
					</td>
					<td width='25' colspan='1' align='right'>
					<input type='button' class='button' value=' > ' onClick='<?php echo "goNextMonth($month,$year,\"$form\",\"$field\")"; ?>'>
					</td>
			</tr>
			<? print_header() ?>
			<tr>
	<?
			for($i = 1; $i < $numDays+1; $i++, $counter++)
			{
					if (strlen($month) == 1)
						$month = "0".$month;

					$timeStamp = strtotime("$year-$month-$i");
					if($i == 1)
					{
					// Workout when the first day of the month is
					$firstDay = date("w", $timeStamp);

					for($j = 0; $j < $firstDay; $j++, $counter++)
					echo "<td> </td>";
					}

					if($counter % 7 == 0)
					echo "</tr><tr>";

					if(date("w", $timeStamp) == 0 || date("w", $timeStamp) == 6)

					$class = "class='weekend'";
					else
					if($i == date("d") && $month == date("m") && $year == date("Y"))
					$class = "class='today'";
					else
					$class = "class='normal'";

					echo "<td class='tr' bgcolor='#ffffff' align='center' width='25'><a class='buttonbar' href='#' onclick=\"sendToForm('$i-$monthlist[$month]-$year','$field','$form');\"><font $class>$i</font></a></td>";
			}
	?>
			</tr>
	</table>
	</td>
</tr>
</table>
</body>
</html>